﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Data;
using BLL;
using System.IO;

public partial class PersonInfo_PersonEdit : BasePage
{
    private Access access = new Access();
    PersonInfoBO pbo = new PersonInfoBO();
    private static int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            
            if (GetQeuryString("para") == "update")
            {
               
                this.txtPersonNo.Enabled = false;
                string id = GetQeuryString("id");
                PersonInfo user = pbo.GetModel(int.Parse(id));
                InitData(user);
            }
            //else if (GetQeuryString("para") == "add")
            //{
            //    txtRHRQ.Text = DateTime.Now.ToString("yyyy-MM-dd");
            //    txtLHRQ.Text = "2099-12-31";
            //    txtSumIntegral.Text = "0";
            //    txtSurplusIntegral.Text = "0";
            //    txtJLJF.Text = "0";
            //    txtGQJF.Text = "0";
            //}
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        PersonInfo user = GetEntity();
         if (GetQeuryString("para") == "update")
        {
            //if (user.ZW != "店长")
            //{
            //    List<PersonInfo> list = pbo.GetModelList(" Superior='" + user.PersonNo + "'");
            //    if (list.Count > 0)
            //    {
            //        string users = "";
            //        foreach (PersonInfo p in list)
            //        {
            //            users += p.Name + ",";
            //        }
            //        users = users.Substring(0, users.Length - 1);
            //        AlertMessage("该员工有下属人员："+users);
            //        return;
            //    }
            //}
            user.ID = Convert.ToInt32(GetQeuryString("id"));
            pbo.Update(user);
            AlertAndCloseAndReloadParendDataGrid("保存成功！", "grid");
        }
    }

    #region 私有方法
    private PersonInfo GetEntity()
    {
        PersonInfo user = new PersonInfo();
        if (GetQeuryString("para") == "update")
        {
            user = pbo.GetModel(Convert.ToInt32(GetQeuryString("id")));
        }
        user.Name = this.txtName.Text;
        user.Sex = this.MAN.Checked ? true : false;
        //user.area = txtArea.Text;
        user.REMARK = txtBZ.Text;
        return user;
    }
    private void InitData(PersonInfo CurrentUserInfo)
    {
        this.txtName.Text = CurrentUserInfo.Name;
        this.txtPhone.Text = CurrentUserInfo.Phone;
        if (CurrentUserInfo.Sex)
            this.MAN.Checked = true;
        else
            this.WOMAN.Checked = true;
        txtPersonNo.Text = CurrentUserInfo.PersonNo;
        txtSurplusIntegral.Text = CurrentUserInfo.SurplusIntegral.ToString();
        txtArea.Text = CurrentUserInfo.area;
        DataSet city = pbo.GetCityCode();
        DataRow[] dr = city.Tables[0].Select(" code='"+CurrentUserInfo.area+"'");
        if (dr.Length > 0)
            txtArea.Text = dr[0]["name"].ToString();
        txtWXH.Text = CurrentUserInfo.FromUserName;
        txtYZM.Text = CurrentUserInfo.YZM;
        txtBirthday.Text = CurrentUserInfo.BIRTHDAY.ToString();
        txtBZ.Text = CurrentUserInfo.REMARK;
        txtCJSJ.Text = CurrentUserInfo.CreateDate.ToString();
        txtDJ.Text = CurrentUserInfo.MEM_LEVEL;
        txtRHRQ.Text = CurrentUserInfo.MEM_DATE.ToString();
        txtWXRQ.Text = CurrentUserInfo.WX_DATE.ToString();
        
        //Image1.ImageUrl = CurrentUserInfo.Photo;
        id = CurrentUserInfo.ID;
    }

    #endregion
    protected void Button2_Click(object sender, EventArgs e)
    {
    //    string FileName = file.FileName;
    //    string path = HttpContext.Current.Request.MapPath("Images/");

    //    if (!string.IsNullOrEmpty(FileName))
    //    {
    //        string extension = System.IO.Path.GetExtension(FileName);//获取文件类型后缀名
    //        if ((extension.ToLower() == ".jpg"))
    //        {
    //            int fileSize = file.PostedFile.ContentLength;
    //            if (fileSize == 0)
    //            {
    //                base.AlertMessage("文件不存在！");
    //            }
    //            //else if (fileSize > 52100)
    //            //{
    //            //    AlertMessage("请上传50K以内的JPG格式照片");
    //            //}
    //            else
    //            {
    //                //if (File.Exists(path + CurrentUserInfo.ID + ".jpg"))
    //                //{
    //                //    File.Delete(path + CurrentUserInfo.ID + ".jpg");
    //                //}
    //                if (true)//限制大小为1M
    //                {
    //                    string guid = Guid.NewGuid().ToString();
    //                    file.SaveAs(path + guid + ".jpg");  //将文件上传到服务器
    //                    if (File.Exists(path + guid + ".jpg"))
    //                    {
    //                       // Image1.ImageUrl = "Images/" + guid + ".jpg";
        //                        txtPicUrl.Text = ConfigurationManager.AppSettings["ServerURL"] +"http://122.226.199.116/Vive/PersonInfo/Images/" + guid + ".jpg";
    //                    }
    //                }
    //            }
    //        }
    //        else
    //        {
    //            AlertMessage("请上传JPG格式图片!");
    //        }
    //    }
    //    else
    //    {
    //        AlertMessage("请选择文件.");
    //    }
    }
}